package Matcher;

import java.util.ArrayList;

import utils.Out;
import documents.Document;

public class JaccardMatcher {

	public float matchDocuments(Document doc1, Document doc2) {
		
		int vereinigungsMenge = 0;
		int dieAndereMenge = doc1.getTokens().size() + doc2.getTokens().size();
		
		ArrayList<String> alreadyMatched = new ArrayList<String>();
		
		int length1 = doc1.getTokens().size();
		int length2 = doc2.getTokens().size();
		
		boolean hasMatch;
		boolean isInMatchedList;
		
		for (int i = 0; i < length1; i++) {
			hasMatch = false;
			String tokenDoc1 = doc1.getTokens().get(i);
				
			/** check if already in list */
			isInMatchedList = false;
			for (int k = 0; k < alreadyMatched.size(); k++) {
				if(tokenDoc1.equals(alreadyMatched.get(k))){
					isInMatchedList = true;
					dieAndereMenge--;
					break;
				}
			}
			if(!isInMatchedList){
				
				for (int j = 0; j < length2; j++) {
					
					if(tokenDoc1.equals(doc2.getTokens().get(j))){
						
						hasMatch = true;
						vereinigungsMenge++;
						alreadyMatched.add(tokenDoc1);
						dieAndereMenge--;
					}
				}
				
			}
		}
		
		Out.println(vereinigungsMenge + " | "+(dieAndereMenge));
		if(dieAndereMenge+vereinigungsMenge == 0) return 0;
		
		return (float)vereinigungsMenge/(float)(dieAndereMenge);
	}
}
